package com.kinghood.productcenter.service.async.handler;

import com.kinghood.productcenter.common.model.dao.product.ProductBaseDO;
import com.kinghood.productcenter.common.model.dto.product.AttributeByProductIdDTO;
import com.kinghood.productcenter.common.model.dto.product.ProductDTO;
import com.kinghood.productcenter.common.model.dto.product.ProductReqDTO;
import com.kinghood.productcenter.common.model.dto.product.UpdateProductLogDTO;
import com.kinghood.productcenter.common.model.enums.product.ProductStatusEnums;
import org.springframework.util.CollectionUtils;

import java.util.Map;
import java.util.Objects;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2024-01-18 10:16
 **/
public class BusinessLogHandler {

    public static void skuAttributeContent(Map<String, String> attributes,
                                           Map<String, AttributeByProductIdDTO> oldSkuAttributeMap,
                                           Map<String, AttributeByProductIdDTO> skuAttributeMap,
                                           StringBuilder sb, Long businessId, Integer businessType) {
        if (CollectionUtils.isEmpty(attributes)) {
            return;
        }
        attributes.forEach((code, value) -> {
            String key = code + businessType + businessId;
            AttributeByProductIdDTO oldOne = oldSkuAttributeMap.get(key);
            AttributeByProductIdDTO newOne = skuAttributeMap.get(key);
            String name = null;
            if(Objects.nonNull(newOne)){
                name = newOne.getAttributeName();
            } else if(Objects.nonNull(oldOne)){
                name = oldOne.getAttributeName();
            }
            BusinessLogHandler.setContent(sb, name, oldOne == null ? null : oldOne.getValueName(),
                    newOne == null ? null : newOne.getValueName());
        });
    }

    /**
     * 修改商品的日志
     */
    public static void handleUpdateProductLog(StringBuilder sb, UpdateProductLogDTO param, Map<String, AttributeByProductIdDTO> attributeMap) {

        ProductReqDTO in = param.getIn();
        ProductDTO oldProduct = param.getOldProductDTO();
        Map<String, AttributeByProductIdDTO> oldAttributeMap = param.getOldProductAttributeMap();
        if (!Objects.equals(in.getProductBase().getStyleName(), oldProduct.getProductBase().getStyleName())) {
            setContent(sb, "款式名称", oldProduct.getProductBase().getStyleName(),
                    in.getProductBase().getStyleName());
        }
        if (!Objects.equals(in.getProductBase().getStatus(), oldProduct.getProductBase().getStatus())) {
            setContent(sb, "款式状态", ProductStatusEnums.getNameByType(oldProduct.getProductBase().getStatus()),
                    ProductStatusEnums.getNameByType(in.getProductBase().getStatus()));
        }
        setContentPicture(sb, oldProduct.getFactoryPicture(), in.getFactoryPicture(), "新增工厂图", "修改工厂图");
        setContentPicture(sb, oldProduct.getHandPicture(), in.getHandPicture(), "新增手拍图", "修改手拍图");
        setContentPicture(sb, oldProduct.getHdPicture(), in.getHdPicture(), "新增高清图", "修改高清图");
        setContentPicture(sb, oldProduct.getOperationPicture(), in.getOperationPicture(), "新增运营图", "修改运营图");
        setContentPicture(sb, oldProduct.getPicture3D(), in.getPicture3D(), "新增3D模型", "修改3D模型");
        setContentPicture(sb, oldProduct.getVideo(), in.getVideo(), "新增视频", "修改视频");

        // 新增或者修改商品属性
        attributeMap.forEach((k, v) -> {
            AttributeByProductIdDTO value = oldAttributeMap.get(k);
            BusinessLogHandler.setContent(sb, v == null ? null : v.getAttributeName(), value == null ? null : value.getValueName(),
                    v == null ? null : v.getValueName());
        });

    }

    /**
     * 图片文案
     */
    public static void setContentPicture(StringBuilder sb, String oldPicture, String newPicture, String nullString, String nonNullString) {
        if (Objects.equals(newPicture, oldPicture)) {
            return;
        }
        if (Objects.isNull(oldPicture)) {
            sb.append(nullString);
        } else {
            sb.append(nonNullString);
        }
    }

    /**
     * 日志文案
     */
    public static void setContent(StringBuilder sb, String row, String old, String replace) {

        if (Objects.equals(old, replace)) {
            return;
        }

        if (Objects.isNull(old)) {
            sb.append("新增").append(row).append(":").append("[").append(replace).append("]");
        } else if(Objects.isNull(replace)){
            sb.append("删除").append(row).append(":").append("[").append(old).append("]");
        } else {
            sb.append(row).append(":从[").append(old).append("]变更为[")
                    .append(replace).append("];");
        }
    }


}
